package com.xingceng.stock.helper;

import com.xingceng.stock.enums.CodeEnum;
import com.xingceng.stock.response.ResponseResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import com.google.common.util.concurrent.RateLimiter;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author:zhengbowen
 * @Date: 2025/11/08 16:59
 * @Description:
 */
@Aspect
@Component
@SuppressWarnings("all")
public class LimiterAspect {
    @Autowired
    private RateLimiter rateLimiter;

    @Pointcut(value = "@annotation(com.xingceng.s)")
    public void pt() {
    }

    @Around(value = "pt()")
    public Object limiter(ProceedingJoinPoint pjp) throws Throwable {
        // 流量
        boolean flag = rateLimiter.tryAcquire();
        if (flag) {
            return pjp.proceed();
        } else {
            return ResponseResult.error(CodeEnum.OVER_CONCURRENT_NUM);
        }
    }
}
